package com.wu.configuration;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wu.bean.RequestException;
import com.wu.bean.exception.*;
import com.wu.util.BaseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName ExceptionHandle
 * @Author wuxuan
 * @Date 2019/3/2 17:25
 * @Version 1.0
 * @Description
 **/
@ControllerAdvice
@Slf4j
public class ExceptionHandle {
    ObjectMapper mapper = new ObjectMapper();


    @ExceptionHandler(value = UserNotFoundException.class)
    @ResponseBody
    public String CatchNotFoundUser(HttpServletRequest request, HttpServletResponse response, Exception e) throws JsonProcessingException {
        RequestException exception = new RequestException(500, "该用户不存在或者密码错误", e.getMessage(), BaseUtil.getUtil().getFormartTime(null));
        log.error("发生了异常,异常信息:" + e.getMessage() + "请求ip:" + BaseUtil.getUtil().getIpAddress(request));
        return mapper.writeValueAsString(exception);
    }

    @ExceptionHandler(value = TokenExpireException.class)
    @ResponseBody
    public String TokenExpire(HttpServletRequest request, HttpServletResponse response, Exception e) throws JsonProcessingException {
        RequestException exception = new RequestException(500, "用户Token过期", e.getMessage(), BaseUtil.getUtil().getFormartTime(null));
        log.error("发生了异常,异常信息:" + e.getMessage() + "请求ip:" + BaseUtil.getUtil().getIpAddress(request));
        return mapper.writeValueAsString(exception);
    }

    @ExceptionHandler(value = FileNotFoundException.class)
    @ResponseBody
    public String FileNotFoundException(HttpServletRequest request, HttpServletResponse response, Exception e) throws JsonProcessingException {
        RequestException exception = new RequestException(500, "文件没有找到", e.getMessage(), BaseUtil.getUtil().getFormartTime(null));
        log.error("发生了异常,异常信息:" + e.getMessage() + "请求ip:" + BaseUtil.getUtil().getIpAddress(request));
        return mapper.writeValueAsString(exception);
    }

    @ExceptionHandler(value = NoTokenException.class)
    @ResponseBody
    public String NoTokenException(HttpServletRequest request, HttpServletResponse response, Exception e) throws JsonProcessingException {
        RequestException exception = new RequestException(500, "Token不存在", e.getMessage(), BaseUtil.getUtil().getFormartTime(null));
        log.error("发生了异常,异常信息:" + e.getMessage() + "请求ip:" + BaseUtil.getUtil().getIpAddress(request));
        return mapper.writeValueAsString(exception);
    }

    @ExceptionHandler(value = PermissionDendException.class)
    @ResponseBody
    public String PermissionDendException(HttpServletRequest request, HttpServletResponse response, Exception e) throws JsonProcessingException {
        RequestException exception = new RequestException(500, "权限不足", e.getMessage(), BaseUtil.getUtil().getFormartTime(null));
        log.error("发生了异常,异常信息:" + e.getMessage() + "请求ip:" + BaseUtil.getUtil().getIpAddress(request));
        return mapper.writeValueAsString(exception);
    }
}
